﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.IO;
using System.Drawing;

namespace UIT.ScrumTool.Client.Utilities.ProjectView
{
    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ConvertToBitmapImage((byte[])value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("The method or operation is not implemented.");
        }

        // Convert byte array to BitmapImage
        public static BitmapImage ConvertToBitmapImage(byte[] byteArray)
        {
            BitmapImage image = new BitmapImage();
            if (byteArray != null && !byteArray.Equals(""))
            {
                MemoryStream stream = new MemoryStream(byteArray);
                image.BeginInit();
                image.StreamSource = stream;
                
                if (image.StreamSource == null || image.StreamSource.Length == 0)
                {
                    image = new BitmapImage(new Uri("/Images/Project/nologo.jpg", UriKind.RelativeOrAbsolute));
                }
                else
                {
                    image.EndInit();
                }
            }
            else
            {
                image = new BitmapImage(new Uri("/Images/Project/nologo.jpg", UriKind.RelativeOrAbsolute));
            }
            return image;
        }

    }
}
